import request from '@renderer/utils/request'
import { AxiosResponse } from 'axios'

// ====================================
// 接口响应通用类型定义
// ====================================

export interface ApiResponse<T = any> {
  code: number
  message: string
  data: T
  timestamp: string
}

// ====================================
// 店铺管理模块类型定义
// ====================================

export interface Shop {
  id: number
  name: string
  code: string
  description: string
  contactPhone: string
  status: 'active' | 'suspended' | 'closed'
  statusName: string
  createdBy: number
  createdByName: string
  createdAt: string
  updatedAt: string
}

export interface CreateShopParams {
  name: string
  code: string
  description?: string
  contactPhone?: string
}

export interface UpdateShopParams {
  id: number
  name?: string
  description?: string
  contactPhone?: string
}

// ====================================
// 店长管理模块类型定义
// ====================================

export interface Manager {
  id: number
  username: string
  realName: string
  phone: string
  email: string
  role: string
  roleName: string
  shopId: number
  shopName: string
  status: 'active' | 'inactive' | 'locked'
  statusName: string
  lastLoginTime: string | null
  lastLoginIp: string | null
  createdAt: string
  updatedAt: string
}

export interface CreateManagerParams {
  shopId: number
  username: string
  password: string
  realName: string
  phone?: string
  email?: string
}

export interface UpdateManagerParams {
  id: number
  realName?: string
  phone?: string
  email?: string
}

// ====================================
// 系统监控模块类型定义
// ====================================

export interface SystemStats {
  statsTime: string
  shopStats: {
    totalCount: number
    activeCount: number
    suspendedCount: number
    closedCount: number
    statusDistribution: {
      active: number
      suspended: number
      closed: number
    }
  }
  userStats: {
    totalUsers: number
    superAdminCount: number
    managerCount: number
    workerCount: number
    activeUsers: number
    inactiveUsers: number
    lockedUsers: number
    roleDistribution: {
      super_admin: number
      manager: number
      worker: number
    }
    statusDistribution: {
      active: number
      inactive: number
      locked: number
    }
  }
  systemStatus: {
    version: string
    startTime: string
    uptime: number
    jvmInfo: {
      javaVersion: string
      jvmName: string
      maxMemory: number
      totalMemory: number
      freeMemory: number
      usedMemory: number
      memoryUsage: number
    }
    databaseStatus: string
  }
}

export interface ShopStats {
  totalCount: number
  activeCount: number
  suspendedCount: number
  closedCount: number
  statusDistribution: {
    active: number
    suspended: number
    closed: number
  }
}

export interface UserStats {
  totalUsers: number
  superAdminCount: number
  managerCount: number
  workerCount: number
  activeUsers: number
  inactiveUsers: number
  lockedUsers: number
  roleDistribution: {
    super_admin: number
    manager: number
    worker: number
  }
  statusDistribution: {
    active: number
    inactive: number
    locked: number
  }
}

export interface SystemStatus {
  version: string
  startTime: string
  uptime: number
  jvmInfo: {
    javaVersion: string
    jvmName: string
    maxMemory: number
    totalMemory: number
    freeMemory: number
    usedMemory: number
    memoryUsage: number
  }
  databaseStatus: string
}

export interface HealthCheck {
  overallHealth: boolean
  databaseHealth: boolean
  memoryHealth: boolean
  systemHealth: boolean
  memoryUsage: number
  uptimeMinutes: number
}

// ====================================
// 店铺管理 API
// ====================================

export const adminShopApi = {
  // 查询所有店铺列表
  getShops(): Promise<AxiosResponse<ApiResponse<Shop[]>>> {
    return request.get('/api/admin/shops')
  },

  // 根据ID查询店铺详情
  getShopById(id: number): Promise<AxiosResponse<ApiResponse<Shop>>> {
    return request.get(`/api/admin/shops/${id}`)
  },

  // 根据状态查询店铺列表
  getShopsByStatus(status: 'active' | 'suspended' | 'closed'): Promise<AxiosResponse<ApiResponse<Shop[]>>> {
    return request.get(`/api/admin/shops/status/${status}`)
  },

  // 创建新店铺
  createShop(params: CreateShopParams): Promise<AxiosResponse<ApiResponse<Shop>>> {
    return request.post('/api/admin/shops', params)
  },

  // 更新店铺基础信息
  updateShop(params: UpdateShopParams): Promise<AxiosResponse<ApiResponse<Shop>>> {
    return request.put('/api/admin/shops', params)
  },

  // 更新店铺状态
  updateShopStatus(id: number, status: 'active' | 'suspended' | 'closed'): Promise<AxiosResponse<ApiResponse<null>>> {
    return request.put(`/api/admin/shops/${id}/status?status=${status}`)
  },

  // 删除店铺
  deleteShop(id: number): Promise<AxiosResponse<ApiResponse<null>>> {
    return request.delete(`/api/admin/shops/${id}`)
  },

  // 检查店铺编号是否存在
  checkShopCode(code: string, excludeId?: number): Promise<AxiosResponse<ApiResponse<boolean>>> {
    let url = `/api/admin/shops/check-code/${code}`
    if (excludeId) {
      url += `?excludeId=${excludeId}`
    }
    return request.get(url)
  }
}

// ====================================
// 店长管理 API
// ====================================

export const adminManagerApi = {
  // 查询所有店长账户列表
  getManagers(): Promise<AxiosResponse<ApiResponse<Manager[]>>> {
    return request.get('/api/admin/managers')
  },

  // 根据店铺ID查询店长账户
  getManagersByShopId(shopId: number): Promise<AxiosResponse<ApiResponse<Manager[]>>> {
    return request.get(`/api/admin/managers/shop/${shopId}`)
  },

  // 根据状态查询店长账户
  getManagersByStatus(status: 'active' | 'inactive' | 'locked'): Promise<AxiosResponse<ApiResponse<Manager[]>>> {
    return request.get(`/api/admin/managers/status/${status}`)
  },

  // 根据ID查询店长账户详情
  getManagerById(id: number): Promise<AxiosResponse<ApiResponse<Manager>>> {
    return request.get(`/api/admin/managers/${id}`)
  },

  // 创建店长账户
  createManager(params: CreateManagerParams): Promise<AxiosResponse<ApiResponse<Manager>>> {
    return request.post('/api/admin/managers', params)
  },

  // 更新店长账户基本信息
  updateManager(params: UpdateManagerParams): Promise<AxiosResponse<ApiResponse<Manager>>> {
    return request.put('/api/admin/managers', params)
  },

  // 更新店长账户状态
  updateManagerStatus(id: number, status: 'active' | 'inactive' | 'locked'): Promise<AxiosResponse<ApiResponse<null>>> {
    return request.put(`/api/admin/managers/${id}/status?status=${status}`)
  },

  // 重置店长密码
  resetManagerPassword(id: number, newPassword: string): Promise<AxiosResponse<ApiResponse<null>>> {
    return request.put(`/api/admin/managers/${id}/password?newPassword=${newPassword}`)
  },

  // 删除店长账户
  deleteManager(id: number): Promise<AxiosResponse<ApiResponse<null>>> {
    return request.delete(`/api/admin/managers/${id}`)
  },

  // 检查用户名是否存在
  checkUsername(username: string, excludeId?: number): Promise<AxiosResponse<ApiResponse<boolean>>> {
    let url = `/api/admin/managers/check-username/${username}`
    if (excludeId) {
      url += `?excludeId=${excludeId}`
    }
    return request.get(url)
  }
}

// ====================================
// 系统监控 API
// ====================================

export const adminSystemApi = {
  // 获取系统整体统计数据
  getSystemStats(): Promise<AxiosResponse<ApiResponse<SystemStats>>> {
    return request.get('/api/admin/system/stats')
  },

  // 获取店铺统计数据
  getShopStats(): Promise<AxiosResponse<ApiResponse<ShopStats>>> {
    return request.get('/api/admin/system/shop-stats')
  },

  // 获取用户统计数据
  getUserStats(): Promise<AxiosResponse<ApiResponse<UserStats>>> {
    return request.get('/api/admin/system/user-stats')
  },

  // 获取系统运行状态
  getSystemStatus(): Promise<AxiosResponse<ApiResponse<SystemStatus>>> {
    return request.get('/api/admin/system/status')
  },

  // 检查数据库连接状态
  getDatabaseStatus(): Promise<AxiosResponse<ApiResponse<string>>> {
    return request.get('/api/admin/system/database-status')
  },

  // 系统健康检查
  getHealthCheck(): Promise<AxiosResponse<ApiResponse<HealthCheck>>> {
    return request.get('/api/admin/system/health')
  }
}

// ====================================
// 统一导出
// ====================================

export default {
  shop: adminShopApi,
  manager: adminManagerApi,
  system: adminSystemApi
}